#include "playmode.h"
#include "ui_playmode.h"

#include <QListWidgetItem>

PlayMode::PlayMode(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PlayMode)
{
    QLayout *layout;


    ui->setupUi(this);
    playList = new PlayList(&QIcon(":/img/play.png"), ui->playListWidget);
    layout = ui->playListWidget->layout();
    layout->addWidget(playList);

    port = NULL;

    mediaObject = new Phonon::MediaObject(this);
    audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
    path = new Phonon::Path;
    path->reconnect(mediaObject, audioOutput);

    progresSlider = new Phonon::SeekSlider(mediaObject, ui->widget);
    layout = ui->widget->layout();
    layout->addWidget(progresSlider);

    volumeSlider = new Phonon::VolumeSlider(audioOutput, ui->widget_2);
    layout = ui->widget_2->layout();
    layout->addWidget(volumeSlider);
    volumeSlider->setMinimumWidth(80);
    volumeSlider->setMaximumWidth(80);


    connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tiknuti(qint64)));
    connect(mediaObject, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(newPalayerState(Phonon::State,Phonon::State)));

    connect(mediaObject, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish()));
    connect(mediaObject, SIGNAL(finished()), this, SLOT(finished()));

    connect(ui->playButton, SIGNAL(clicked()), this, SLOT(play()));
    connect(ui->stopButton, SIGNAL(clicked()), mediaObject, SLOT(stop()));
    connect(ui->pauseButton, SIGNAL(clicked()), mediaObject, SLOT(pause()));

    connect(playList, SIGNAL(songActivated(QString)), this, SLOT(songActivated(QString)));

    connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addToPlayList()));
    connect(ui->RemoveButton, SIGNAL(clicked()), playList, SLOT(removeSongs()));
    connect(ui->nextButton, SIGNAL(clicked()), this, SLOT(nextSong()));
    connect(ui->previousButton, SIGNAL(clicked()), this, SLOT(previousSong()));


    QList<Phonon::AudioOutputDevice> devices = Phonon::BackendCapabilities::availableAudioOutputDevices();
    for(int i = 0; i < devices.size(); i++)
    {
        QString str = devices[i].name();
        ui->audoDeviceCombo->addItem(str);
    }

    connect(ui->audioDeviceApplyButton, SIGNAL(clicked()), this, SLOT(changeAudioDevice()));

    mediaObject->setTickInterval(10);
}

void PlayMode::changeAudioDevice()
{
    QList<Phonon::AudioOutputDevice> devices = Phonon::BackendCapabilities::availableAudioOutputDevices();
    audioOutput->setOutputDevice(devices[ui->audoDeviceCombo->currentIndex()]);
}

void PlayMode::tiknuti(qint64 h)
{
    ui->soundTimeLabel->setText(QString("%1:%2/%3:%4")
                                .arg((h/60000)%60)
                                .arg((h/1000)%60)
                                .arg((mediaObject->totalTime()/60000)%60)
                                .arg((mediaObject->totalTime()/1000)%60)
                                );
}

PlayMode::~PlayMode()
{
    delete ui;
    delete playList;
    delete mediaObject;
    delete audioOutput;
    delete path;
    delete progresSlider;
    delete volumeSlider;

}


void PlayMode::setPort(QtSeriovyPort *port)
{
    this->port = port;
}



void PlayMode::newPalayerState(Phonon::State newstate, Phonon::State oldstate)
{
    if(newstate == Phonon::LoadingState)
    {

    }
    else if(newstate == Phonon::StoppedState)
    {
        ui->playButton->setDisabled(false);
        ui->playButton->setIcon(QIcon(":/img/play.png"));
        ui->stopButton->setDefault(true);
        ui->stopButton->setIcon(QIcon(":/img/stop-selected.png"));
        ui->pauseButton->setDisabled(true);
        ui->pauseButton->setIcon(QIcon(":/img/pause.png"));
        ui->soundTimeLabel->setText(QString("%1:%2/%3:%4")
                                    .arg(0)
                                    .arg(0)
                                    .arg((mediaObject->totalTime()/60000)%60)
                                    .arg((mediaObject->totalTime()/1000)%60)
                                    );
    }
    else if(newstate == Phonon::PlayingState)
    {
        ui->playButton->setDisabled(true);
        ui->playButton->setIcon(QIcon(":/img/play-selected.png"));
        ui->stopButton->setDefault(false);
        ui->stopButton->setIcon(QIcon(":/img/stop.png"));
        ui->pauseButton->setDisabled(false);
        ui->pauseButton->setIcon(QIcon(":/img/pause.png"));
    }
    else if(newstate == Phonon::PausedState)
    {
        ui->playButton->setDisabled(false);
        ui->playButton->setIcon(QIcon(":/img/play.png"));
        ui->stopButton->setDefault(false);
        ui->stopButton->setIcon(QIcon(":/img/stop.png"));
        ui->pauseButton->setDisabled(true);
        ui->pauseButton->setIcon(QIcon(":/img/pause-selected.png"));
    }
}

void PlayMode::addToPlayList()
{
    QFileDialog dialog;
    dialog.setNameFilter(tr("Sounds (*.mp3 *.wav *.midi *.ogg *.wma)"));
    dialog.setFileMode(QFileDialog::ExistingFiles);
    if(dialog.exec())
    {
        playList->addFiles(&dialog.selectedFiles());
    }
}

void PlayMode::aboutToFinish()
{

}

void PlayMode::finished()
{
    QString str = playList->nextSong();
    if(str.isEmpty())
    {
        return;
    }

    mediaObject->clear();
    mediaObject->setCurrentSource(Phonon::MediaSource(str));
    mediaObject->play();
}

void PlayMode::play()
{
    if(mediaObject->state() == Phonon::PausedState)
    {
        mediaObject->play();
        return;
    }
    QString str = playList->getSong();
    if(str.isEmpty())
    {
        return;
    }
    mediaObject->setCurrentSource(Phonon::MediaSource(str));
    mediaObject->play();
}

void PlayMode::pause()
{
    mediaObject->pause();
}

void PlayMode::nextSong()
{
    QString str = playList->nextSong();
    if(str == QString())
    {
        mediaObject->stop();
        return;
    }
    mediaObject->clear();
    mediaObject->setCurrentSource(str);
    mediaObject->play();
}

void PlayMode::previousSong()
{
    QString str = playList->previousSong();
    if(str == QString())
    {
        mediaObject->stop();
        return;
    }
    mediaObject->clear();
    mediaObject->setCurrentSource(str);
    mediaObject->play();
}

void PlayMode::stop()
{

}

void PlayMode::songActivated(QString filePath)
{
    mediaObject->clear();
    mediaObject->setCurrentSource(Phonon::MediaSource(filePath));
    mediaObject->play();
}

void PlayMode::savePlayListAs()
{
    QFileDialog dialog;
    dialog.setFileMode(QFileDialog::AnyFile);
    dialog.setAcceptMode(QFileDialog::AcceptSave);
    dialog.setFilter(tr("Tabor Play List (*.tpls)"));
    dialog.setDefaultSuffix("tpls");
    if(dialog.exec())
    {
        QStringList list;
        list = dialog.selectedFiles();
        strPlayList = list[0];
        playList->savePlayList(strPlayList);
    }
}

void PlayMode::savePlayList()
{
    if(strPlayList == QString())
    {
        savePlayListAs();
        return;
    }
    playList->savePlayList(strPlayList);
}

void PlayMode::openPlayList()
{
    QFileDialog dialog;
    dialog.setNameFilter(tr("Tabor Play List (*.tpls)"));
    dialog.setFileMode(QFileDialog::ExistingFiles);
    if(dialog.exec())
    {
        QStringList list;
        list = dialog.selectedFiles();
        playList->openPlayList(list[0]);
    }
}

void PlayMode::newPlayList()
{
    playList->clear();
}
